home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / hips / sources / tools / to_hjpeg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-25  |  3.2 KB  |  127 lines

  1. /*
  2.  * Copyright (c) 1992    Jin Guojun
  3.  *
  4.  * Disclaimer:  No guarantees of performance accompany this software,
  5.  * nor is any responsibility assumed on the part of the authors.  All the
  6.  * software has been tested extensively and every effort has been made to
  7.  * insure its reliability.
  8.  *
  9.  * to_hjpeg - convert an image to HIPS-JPEG format
  10.  *
  11.  * usage:    to_hjpeg < idata > oseq
  12.  *
  13.  * to load:    cc -o to_hjpeg to_hjpeg.c -hipsh -lhips
  14.  *
  15.  * AUTHOR:    Jin Guojun - 11/12/92
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <hipl_format.h>
  20.  
  21. static Flag_Format flagfmt[] =
  22. {
  23.     {"c", {LASTFLAG}, 0, {{PTBOOLEAN, "FALSE"}, LASTPARAMETER}},
  24.     {"o", {LASTFLAG}, 0, {{PTBOOLEAN, "FALSE"}, LASTPARAMETER}},
  25.     {"Q", {LASTFLAG}, 1, {{PTINT, "75", "Q-factor"}, LASTPARAMETER}},
  26.     LASTFLAG};
  27.  
  28. main(argc, argv)
  29. int       argc;
  30. char    **argv;
  31.  
  32. {
  33. int    oform, qfact;
  34. Boolean    oflag, ccflag, cflag;
  35. int    rows, cols, frames, f, numcolor, pad;
  36. int    xstream[2], xjpeg[2];
  37. struct header    hd;
  38. sframe_header    fhd;
  39. Filename    filename;
  40. FILE    *fp;
  41. byte    *fakec;
  42.  
  43.     Progname = strsave(*argv);
  44.     parseargs(argc, argv, flagfmt, &ccflag, &oflag, &qfact, FFONE, &filename);
  45.  
  46.     fp = hfopenr(filename);
  47. /*
  48.     fread_hdr_a(fp, &hd, filename);
  49. */
  50.     cflag = hjpeg_uimg_init(fp, &hd, 1, 1);
  51.     frames = hd.num_frame;
  52.     numcolor = hd.numcolor;
  53.  
  54.     xstream[0] = 1;
  55.     xstream[1] = 1;
  56.     setparam(&hd, "stream-info", PFINT, 2, xstream);
  57.     xjpeg[0] = qfact;
  58.     xjpeg[1] = -1;        /* place to store data size later */
  59.     setparam(&hd, "JPEG-info", PFINT, 2, xjpeg);
  60.     write_headeru(&hd, argc, argv);
  61.     ccflag &= !cflag;
  62.     check_compress_header(stdout, &hd, 1, cflag | ccflag);
  63.  
  64.     alloc_image(&hd);
  65.     if (ccflag && !oflag)    /* fake color image for gray-scale */
  66.     fakec = (byte *) malloc(hd.cols * hd.rows * 3);
  67.     else
  68.     fakec = hd.image;
  69.  
  70.     for (f = 0; f < frames * numcolor; f++) {
  71. #ifdef OLD
  72.     fread_image(fp, &hd, f, filename);
  73. #else
  74. /*
  75.     if (fread(hd.image, hd.sizeimage, 1, fp) != 1)
  76.         return (perr(HE_READFRFILE, f, filename));
  77. */
  78.     hjpeg_read_image(fakec);
  79. #endif
  80.     if (!oflag) {
  81.         if (ccflag) {
  82.         register byte *cp = fakec, *sp = hd.image;
  83.         register int r = hd.rows, w = hd.cols;
  84.         while (r--) {
  85.             memcpy(cp, sp, w);
  86.             cp += w;
  87.             memcpy(cp, sp, w);
  88.             cp += w;
  89.             memcpy(cp, sp, w);
  90.             cp += w;
  91.             sp += w;
  92.         }
  93.         }
  94.         (int) hd.image ^= (int) fakec ^= (int) hd.image ^= (int) fakec;
  95.         if (compress_jpeg_frame(&hd, &fhd) < 0)
  96.         return (perr(HE_WRITEFRFILE, f, filename));
  97.         /*
  98.          * this routine points hd->image to the compressed image, and
  99.          * sets the size of the compressed frame in fhd
  100.          */
  101.         fhd.frame = f;
  102.         pad = 8 - (fhd.size % 8);
  103.         fprintf(stderr, "padding by %d bytes \n", pad);
  104.         fhd.size += pad;
  105.         if (fwrite(&fhd, sizeof(sframe_header), 1, stdout) != 1)
  106.         return (perr(HE_WRITEFRFILE, f, filename));
  107.         if (fwrite(hd.image, fhd.size, 1, stdout) != 1)
  108.         return (perr(HE_WRITEFRFILE, f, filename));
  109.         (int) hd.image ^= (int) fakec ^= (int) hd.image ^= (int) fakec;
  110.  
  111. #define NEED            /* this is needed for compatibility with the
  112.                  * Parallax card */
  113. #ifdef NEED
  114.         while (pad--)
  115.         fputc(0, stdout);
  116. #endif
  117.     } else
  118.         write_image(&hd, f);
  119.     }
  120.     write_jpeg_eof(stdout);
  121.     fclose(stdout);
  122.     free(hd.image);
  123.     if (cflag)
  124.     free(fakec);
  125.     return (0);
  126. }
  127.